package com.youyi.flymewallpager.ui;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.trello.rxlifecycle.ActivityEvent;
import com.youyi.flymewallpager.R;
import com.youyi.flymewallpager.apdater.BaseRecyclerAdapter;
import com.youyi.flymewallpager.apdater.WallPagerAdapter;
import com.youyi.flymewallpager.decoration.DividerItemDecoration;
import com.youyi.flymewallpager.entity.WallPager;
import com.youyi.flymewallpager.wallpager.SimpleWallPager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by xzp on 16/6/22.
 */
public class UIMain extends UIBase implements BaseRecyclerAdapter.OnItemClickListener{

    @BindView(R.id.wall_pager_list_recycler)
    RecyclerView mRecycler;
    private WallPagerAdapter mAdapter;
    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new WallPagerAdapter(this);
        mRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mRecycler.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(this,R.drawable.normal_light_divider)));
        Observable.create(new Observable.OnSubscribe<List<WallPager>>() {
            @Override
            public void call(Subscriber<? super List<WallPager>> subscriber) {
                try {
                    InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open("wallPagerList"));
                    BufferedReader bufReader = new BufferedReader(inputReader);
                    String line="";
                    StringBuilder result=new StringBuilder();
                    while((line = bufReader.readLine()) != null)
                        result.append(line);
                    List<WallPager> list=JSON.parseArray(result.toString(),WallPager.class);
                    if(!subscriber.isUnsubscribed()){
                        subscriber.onNext(list);
                    }
                }catch (IOException exception){

                }
                if(!subscriber.isUnsubscribed())
                    subscriber.unsubscribe();

            }
        }).compose(this.<List<WallPager>>bindUntilEvent(ActivityEvent.DESTROY)).observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.newThread())
        .subscribe(new Action1<List<WallPager>>() {
            @Override
            public void call(List<WallPager> list) {
                mAdapter.setList(list);
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        WallPager wallPager=mAdapter.getList().get(position);
        if(TextUtils.equals(wallPager.getClassName(), SimpleWallPager.TAG)){
            startActivity(new Intent(this,UISimpleSettings.class));
        }
//        else {
//        try {
//            Intent intent = new Intent(
//                    WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
//            intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
//                    new ComponentName(this, Class.forName(getPackageName()+".wallpager."+wallPager.getClassName())));
//            startActivity(intent);
//        }catch (ClassNotFoundException exception){
//
//        }
    }
}
